// USE the Flickr API Wrapper to get the infromation

package org.unc.linamania;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.tags.Cluster;
import com.aetrion.flickr.tags.ClusterList;
import com.aetrion.flickr.tags.HotlistTag;
import com.aetrion.flickr.tags.RelatedTagsList;
import com.aetrion.flickr.tags.Tag;
import com.aetrion.flickr.tags.TagsInterface;

public class FlickrController {

	static String API_KEY = "631188e1e9091309cf3590388dba7891";
	static String TIME_PERIOD = "alltime";

	private Flickr flickr;
	private TagsInterface ti;

	public FlickrController() {
		this.flickr = new Flickr(API_KEY);
		this.ti = this.flickr.getTagsInterface();
	}

	// Give popular tags for lina
	public List<FlickrTag> getHotTags() {
		List<FlickrTag> tagList = new ArrayList<FlickrTag>();
		Collection<Tag> hotList;
		try {
			//hotList = this.ti.getHotList(TIME_PERIOD, 25);
			hotList = ti.getListUser("98055892@N00");
			Iterator<Tag> it = hotList.iterator();
			int i = 0;
		    while (it.hasNext() && i < 25 )
		    {
		    	Tag hl = it.next();
		    	FlickrTag tag = new FlickrTag(hl.getValue());
		    	tagList.add(tag);
		    	i++;
		    }
//			for (Tag hl : hotList) {
//				FlickrTag tag = new FlickrTag(hl.getValue());
//				tag.setName(hl.getValue());
//				tagList.add(tag);
//			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return tagList;
	}

	// Given a tag, give me photos
	public List<FlickrPhoto> getClusteredPhotos(FlickrTag tag) {
		List<FlickrPhoto> finalphotoList = new ArrayList<FlickrPhoto>();
		PhotoList photoList;
		try {
			photoList = ti.getClusterPhotos(tag.getName(), tag.getName());
			for (Object o : photoList) {
				Photo photo = (Photo) o;
				FlickrPhoto fphoto = new FlickrPhoto();
				fphoto.setId(photo.getId());
				fphoto.setURL(photo.getLargeUrl());
				fphoto.setSize(photo.getOriginalWidth(), photo
						.getOriginalHeight());
				fphoto.setTitle(photo.getTitle());
				Collection<Tag> coll = photo.getTags();
				List<String> tags = new ArrayList<String>();
				for (Tag t : coll) {
					tags.add(t.getValue());
				}
				fphoto.setTags(tags);
				finalphotoList.add(fphoto);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return finalphotoList;

	}

	// Given a photo, give me its tags
	public List<FlickrTag> getPhotoTags(FlickrPhoto photo) {
		List<FlickrTag> tagList = new ArrayList<FlickrTag>();
		Photo photo_api;
		try {
			photo_api = this.ti.getListPhoto(photo.getId());
			Iterator<Tag> it = photo_api.getTags().iterator();
			while (it.hasNext()) {
				Tag tag = it.next();
				FlickrTag ftag = new FlickrTag(tag.getValue());
	//			ftag.setName(tag.getValue());
				tagList.add(ftag);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tagList;
	}

	// Given a tag, give cluster tags
	public List<List<String>> getClusterTags(FlickrTag ftag) {
		List<List<String>> finaltagList = new ArrayList<List<String>>();
		try {
			ClusterList clist = ti.getClusters(ftag.getName());
			List<Cluster> c = clist.getClusters();
			for (Cluster cluster : c) {
					List<Tag> lt = cluster.getTags();
					List<String> taglist = new ArrayList<String>();
					for(Tag t : lt) {
						taglist.add(t.getValue());
					}
					finaltagList.add(taglist);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return finaltagList;
	}

	// Given a tag, give related tags
	public List<FlickrTag> getRelatedTags(FlickrTag ftag) {
		List<FlickrTag> finaltagList = new ArrayList<FlickrTag>();
		RelatedTagsList tagList;
		try {
			tagList = ti.getRelated(ftag.getName());
			ti.getClusters(ftag.getName());
			Iterator<Tag> it = tagList.iterator();
			while (it.hasNext()) {
				Tag tag = it.next();
				FlickrTag flickrtag = new FlickrTag(tag.getValue());
//				flickrtag.setName(tag.getValue());
				finaltagList.add(flickrtag);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return finaltagList;

	}
	
	public static void main(String[] args) {
		FlickrController c = new FlickrController();
		FlickrTag f = new FlickrTag("shark");
	//	f.setName("shark");
		
		//get cluster tags
		System.out.println("CLUSTER TAGS");
		List<List<String>> l = c.getClusterTags(f);
		for(List<String> sl : l) {
			for(String s : sl) {
				System.out.println(s);
			}
			//System.out.println();
		}
		
		//Get Lina popular tags
		System.out.println("LINA POPULAR TAGS");
		List<FlickrTag> hl = c.getHotTags();
		for(FlickrTag ft : hl) {
			System.out.println(ft.getName());
		}
		
		//Get photo tags
		System.out.println("PHOTO TAGS");
		FlickrPhoto photo = new FlickrPhoto();
		photo.setId("3461810848");
		List<FlickrTag> pt = c.getPhotoTags(photo);
		for(FlickrTag ft : pt) {
			System.out.println(ft.getName());
		}
		
		//Get related tags
		System.out.println("RELATED TAGS");
		List<FlickrTag> rt = c.getRelatedTags(f);
		for(FlickrTag ft : rt) {
			System.out.println(ft.getName());
		}
		
		// Get photos
		System.out.println("PHOTOS");
		List<FlickrPhoto> ph = c.getClusteredPhotos(f);
		for(FlickrPhoto fphoto : ph) {
			System.out.println(fphoto.getURL());
		}
	}

}